import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:im_app/im/util/DbUtil.dart';

import '../../dao/ImGroupUserDao.dart';
import '../../pojo/ImPojo.dart';
import '../../service/ImSessionService.dart';
import '../../socket/dto/business/SendData.dart';
import '../../socket/service/RpcSendDataService.dart';
import '../../util/DateUtil.dart';
import '../../util/ImConstant.dart';
import 'SessionListCubit.dart';

final class SessionSettingCubit extends Bloc<SessionSettingEvent, SessionSettingState> {
  final ImSession session;
  final SessionListCubit sessionListCubit;

  SessionSettingCubit({required this.session, required this.sessionListCubit}) :
        super(SessionSettingState(session: session, random: DateUtil.nowTime())) {
    on<SessionSettingTopEvent>((event, emit){
      _sessionTop(state, event.isTop);
    });
    on<SessionSettingDisturbEvent>((event, emit){
      _sessionDisturb(state, event.isDisturb);
    });
    on<SessionSettingShowNameEvent>((event, emit){
      _sessionShowName(state, event.isShowName);
    });
  }

  void _sessionTop(SessionSettingState oldState, bool isTop) async {
    await sessionListCubit.sessionTop(oldState.session, isTop);
    emit(oldState.copy(random: DateUtil.nowTime()));
  }

  void _sessionDisturb(SessionSettingState oldState, bool isDisturb) async {
    await sessionListCubit.sessionDisturb(oldState.session, isDisturb);
    var newState = oldState.copy(random: DateUtil.nowTime());
    emit(newState);
  }

  void _sessionShowName(SessionSettingState oldState, bool isShowName) async {
    var rpcMessageService = RpcMessageService();

    var session = oldState.session;
    var groupUser = session.group!.currentGroupUser!;
    groupUser.isShowName = isShowName;
    groupUser.updateTime = DateTime.now();

    var key = session.group!.code + ImConstant.MESSAGE_SPLIT + groupUser.userId;
    var dataChangeReq = DataChangeReq(type: DataType.groupUser,
        subType: DataSubType.gu_ed_showName,
        value: groupUser.isShowName.toString(),
        key: key);
    await rpcMessageService.sendDataChange(dataChangeReq);

    var groupUserDao = ImGroupUserDao();
    await groupUserDao.batchUpdates([groupUser].toSet());

    emit(oldState.copy(random: DateUtil.nowTime()));
  }
}

final class SessionSettingState extends Equatable {

  final ImSession session;
  final int random;

  const SessionSettingState({
    required this.session,
    required this.random,
  });

  SessionSettingState copy({ImSession? session, int? random}) {
    return SessionSettingState(
      session: session ?? this.session,
      random: random ?? this.random,
    );
  }

  @override
  List<Object?> get props => [session, random];

}

class SessionSettingEvent {}
class SessionSettingTopEvent extends SessionSettingEvent {
  bool isTop;
  SessionSettingTopEvent(this.isTop);
} // 会话置顶
class SessionSettingDisturbEvent extends SessionSettingEvent {
  bool isDisturb;
  SessionSettingDisturbEvent(this.isDisturb);
} // 会话免打扰
class SessionSettingShowNameEvent extends SessionSettingEvent {
  bool isShowName;
  SessionSettingShowNameEvent(this.isShowName);
}
class SessionSettingWakingEvent extends SessionSettingEvent {
  bool isWaking;
  SessionSettingWakingEvent(this.isWaking);
}